package com.lin.learning.questionmanagement.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.learning.questionmanagement.mapper.TeacherMapper;
import com.lin.learning.questionmanagement.mapper.TeacherVoMapper;
import com.lin.learning.questionmanagement.service.TeacherVoService;
import com.lin.learning.questionmanagement.vo.PageVo;
import com.lin.learning.questionmanagement.vo.TeacherVo;
import com.lin.learning.questionmanagement.vo.TeacherWithName;
import com.our.learning.infrastructure.po.Clazz;
import com.our.learning.infrastructure.po.Teacher;
import com.our.learning.infrastructure.pojo.ResponseResult;
import com.studentmanagement.mapper.ClassMapper;
import com.studentmanagement.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TeacherVoServiceImpl extends ServiceImpl<TeacherVoMapper, TeacherVo> implements TeacherVoService {

    @Autowired
    private ClassMapper classMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private  TeacherVoMapper teacherVoMapper;

    @Override
    public ResponseResult teacherList(Integer page, Integer perPage,String name) {
        if(name!=null){
            LambdaQueryWrapper<TeacherVo> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.like(TeacherVo::getName,name);
            lambdaQueryWrapper.orderByDesc(TeacherVo::getTeacherId);
            //List<TeacherVo> list=teacherVoMapper.selectList(lambdaQueryWrapper);
/*            TeacherVo teacher= teacherMapper.selectByTeacher_name(name);
            List<TeacherVo> list=new ArrayList<>();
            list.add(teacher);*/
            Page<TeacherVo> pages=new Page<>(page,perPage);
            page(pages,lambdaQueryWrapper);
            List<TeacherVo> list=pages.getRecords();
            List<TeacherWithName> nameList=new ArrayList<>(list.size());
            for (int i=0;i<pages.getTotal();i++){
                String names = classMapper.selectById(list.get(i).getClassId()).getName();
                TeacherWithName teacher=new TeacherWithName(list.get(i).getTeacherId(),list.get(i).getName(),list.get(i).getClassId(),list.get(i).getPhone(),names);
                nameList.add(teacher);
            }
            PageVo pageVo=new PageVo(nameList,pages.getTotal());
            return ResponseResult.okResult(pageVo);
        }
        LambdaQueryWrapper<TeacherVo> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(TeacherVo::getTeacherId);
        Page<TeacherVo> pages=new Page<>(page,perPage);
        page(pages,lambdaQueryWrapper);
        List<TeacherVo> list=pages.getRecords();
        List<TeacherWithName> nameList=new ArrayList<>(list.size());
        for (int i=0;i<pages.getTotal();i++){
            String names = classMapper.selectById(list.get(i).getClassId()).getName();
            TeacherWithName teacher=new TeacherWithName(list.get(i).getTeacherId(),list.get(i).getName(),list.get(i).getClassId(),list.get(i).getPhone(),names);
            nameList.add(teacher);
        }
        PageVo pageVo=new PageVo(nameList,pages.getTotal());
        return ResponseResult.okResult(pageVo);
    }
}
